package com.markus.code.newcode;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/4 20:49
 * todo:
 */
public class Sqrt {
    /**
     *
     * @param x int整型
     * @return int整型
     */
    public int sqrt (int x) {
        // write code here
        if(x == 0){
            return 0;
        }
        int left = 1 , right = x;
        while(left <= right){
            long mid = left + ((right-left) >> 1);
            if ( mid * mid <= x && (mid+1) * (mid+1) > x){
                return (int) mid;
            }else if (mid * mid <= x){
                left = (int) mid;
            }else {
                right = (int) mid;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int n = 1;
        Sqrt demo = new Sqrt();
        System.out.println(demo.sqrt(n));
    }
}
